/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.events;

import app.dtos.status.MemberStatus;
import app.models.member.Member;
import app.models.wx.WxTicket;
import com.google.common.eventbus.Subscribe;
import com.jfinal.module.wxchat.utils.HttpUtil;

import java.io.File;

/**
 * <p>
 * EventBus 总线处理.
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-12 12:29
 * @since JDK 1.6
 */
public class EventBusListener {


    @Subscribe
    public void wxImageDownload(ImageDownloadEDto dto) {
        try {
            File file = HttpUtil.download(dto.getUrl(), dto.getSaveRealPath());

            String fileName = file.getName();
            String suffix = "";
            int index = fileName.lastIndexOf('.');
            if (index > -1) {
                suffix = fileName.substring(index);
            }

            WxTicket ticket = new WxTicket();
            ticket.set("pricute", dto.getSavePath() + suffix);
            ticket.set("member", dto.getMemberId());
            ticket.set("wx_account", dto.getWxAccount());
            ticket.set("upload_time", dto.getReceTime());
            ticket.save();

            //更改会员状态
            if (dto.getMemberStatus() != MemberStatus.OFFICIAL) {
                Member member = new Member();
                member.set("id", dto.getMemberId());
                member.set("status", MemberStatus.OFFICIAL);

                member.update();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
